/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 邓大帅
 * Date: 2022-12-17
 * Time: 14:53
 */
public class BinarySearch {
    public static int binarySearch(int[] array,int n) {
        int i = 0;
        int j = array.length - 1;
        while (i < j) {
            int k = (i + j) / 2;
            if (array[k] == n) {
                return k;
            }
            if (array[k] > n) {
                j = --k;
            }
            if (array[k] < n) {
                i = ++k;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] array = new int[]{0,1,2,3,4,5,6,7,8,9};
        int index = binarySearch(array,4);
        System.out.println(index);
    }
}
